<template>
  <div class="goodslist_container">
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索栏 -->
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input placeholder="请输入内容" v-model="querys.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table border stripe :data="goodsList" style="width: 100%">
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="92px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px">
        </el-table-column>
        <el-table-column label="创建时间" width="140px">
          <template slot-scope="scope">
            <!-- 使用時間過濾器 -->
            <!-- TODO: 全是同一個時間？？1970？？ -->
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!--  slot-scope="scope" -->
            <el-button size="mini" type="warning" icon="el-icon-edit"></el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="delGoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="querys.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GoodsListPage',
  data () {
    return {
      // 请求参数
      querys: {
        query: '', // 查询参数
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodsList: [],
      // 总数据条数
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.querys
      })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败！')
      console.log(res.data)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 改变每页显示数量
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.querys.pagesize = newSize
      this.getGoodsList()
    },
    // 改变当前页
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.querys.pagenum = newPage
      this.getGoodsList()
    },
    // 删除商品
    async delGoods (id) {
      const result = await this.$confirm('是否确认删除该商品？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)

      if (result !== 'confirm') return this.$message.info('已取消删除！')
      // 点击确认得到 confirm
      // 发送请求
      const { data: res } = await this.$http.delete(`goods/${id}`)
      console.log(res)
      // 判断是否请求响应陈功
      if (res.meta.status !== 200) return this.$message('操作失败，请稍后重试！')
      // 重新加载列表
      this.getGoodsList()
      // 提示操作成功
      this.$message.success('删除成功!')
    },
    // 跳轉添加商品頁面
    goAddPage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
